home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / prim / fill.el < prev    next >
Encoding:
Text File  |  1995-05-12  |  16.2 KB  |  429 lines

  1. ;;; fill.el --- fill commands for XEmacs.
  2.  
  3. ;; Copyright (C) 1985, 1986, 1992, 1993, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Keywords: wp
  6.  
  7. ;; This file is part of XEmacs.
  8.  
  9. ;; XEmacs is free software; you can redistribute it and/or modify it
  10. ;; under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; XEmacs is distributed in the hope that it will be useful, but
  15. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. ;; General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  21. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. ;;; Synched up with: FSF 19.28.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;; All the commands for filling text.  These are documented in the XEmacs
  28. ;; Reference Manual.
  29.  
  30. ;;; Code:
  31.  
  32. (defvar fill-individual-varying-indent nil
  33.   "*Controls criterion for a new paragraph in `fill-individual-paragraphs'.
  34. Non-nil means changing indent doesn't end a paragraph.
  35. That mode can handle paragraphs with extra indentation on the first line,
  36. but it requires separator lines between paragraphs.
  37. A value of nil means that any change in indentation starts a new paragraph.")
  38.  
  39. (defvar sentence-end-double-space t
  40.   "*Non-nil means a single space does not end a sentence.")
  41.  
  42. (defun set-fill-prefix ()
  43.   "Set the fill prefix to the current line up to point.
  44. Filling expects lines to start with the fill prefix
  45. and reinserts the fill prefix in each resulting line."
  46.   (interactive)
  47.   (setq fill-prefix (buffer-substring
  48.              (save-excursion (beginning-of-line) (point))
  49.              (point)))
  50.   (if (equal fill-prefix "")
  51.       (setq fill-prefix nil))
  52.   (if fill-prefix
  53.       (message "fill-prefix: \"%s\"" fill-prefix)
  54.     (message "fill-prefix cancelled")))
  55.  
  56. (defvar adaptive-fill-mode t
  57.   "*Non-nil means determine a paragraph's fill prefix from its text.")
  58.  
  59. ;; #### - this is still weak.  Yeah, there's filladapt, but this should
  60. ;; still be better...  --Stig
  61. (defvar adaptive-fill-regexp (purecopy "[ \t]*\\([#;>*]+ +\\)?")
  62.   "*Regexp to match text at start of line that constitutes indentation.
  63. If Adaptive Fill mode is enabled, whatever text matches this pattern
  64. on the second line of a paragraph is used as the standard indentation
  65. for the paragraph.")
  66.  
  67. ;; XEmacs - this is pulled out of fill-region-as-paragraph so that it can
  68. ;; also be called from do-auto-fill   
  69. (defun maybe-adapt-fill-prefix (&optional from to skip-first)
  70.   (if (and adaptive-fill-mode
  71.        (or (null fill-prefix) (string= fill-prefix "")))
  72.       (save-excursion
  73.     (or from (setq from (save-excursion (beginning-of-line) (point))))
  74.     (or to   (setq to (save-excursion (beginning-of-line 2) (point))))
  75.     (goto-char (min from to))
  76.     (if (eolp) (forward-line 1))
  77.     (and skip-first (forward-line 1))
  78.     (if (< (point) (max from to))
  79.         (let ((start (point)))
  80.           (re-search-forward adaptive-fill-regexp)
  81.           (setq fill-prefix (buffer-substring start (point))))
  82.       (goto-char (min from to))
  83.       (if (eolp) (forward-line 1))
  84.       ;; If paragraph has only one line, don't assume in general
  85.       ;; that additional lines would have the same starting
  86.       ;; decoration.  Assume no indentation.
  87.       ;; But if left-margin is nonzero, we can assume ordinary
  88.       ;; lines do have indentation.
  89.       ;; #### - fa-extras.el comments this out, but I think it's okay,
  90.       ;; given the defaults for FROM and TO.  --Stig 
  91.       (if (> left-margin 0)
  92.           (progn
  93.         (re-search-forward adaptive-fill-regexp)
  94.         (setq fill-prefix (make-string (current-column) ?\ ))))
  95.       ))))
  96.  
  97. (defun fill-region-as-paragraph (from to &optional justify-flag)
  98.   "Fill region as one paragraph: break lines to fit `fill-column'.
  99. Prefix arg means justify too.
  100. If `sentence-end-double-space' is non-nil, then period followed by one
  101. space does not end a sentence, so don't break a line there.
  102. From program, pass args FROM, TO and JUSTIFY-FLAG."
  103.   (interactive "*r\nP")
  104.   ;; Arrange for undoing the fill to restore point.
  105.   (if (and buffer-undo-list (not (eq buffer-undo-list t)))
  106.       (setq buffer-undo-list (cons (point) buffer-undo-list)))
  107.   ;; Don't let Adaptive Fill mode alter the fill prefix permanently.
  108.   (let ((fill-prefix fill-prefix))
  109.     ;; Figure out how this paragraph is indented, if desired.
  110.     (maybe-adapt-fill-prefix from to t)
  111.     (save-restriction
  112.       (narrow-to-region from to)
  113.       (goto-char (point-min))
  114.       (skip-chars-forward "\n")
  115.       (narrow-to-region (point) (point-max))
  116.       (setq from (point))
  117.       (goto-char (point-max))
  118.       (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
  119.                (regexp-quote fill-prefix))))
  120.     ;; Delete the fill prefix from every line except the first.
  121.     ;; The first line may not even have a fill prefix.
  122.     (and fpre
  123.          (progn
  124.            (if (>= (length fill-prefix) fill-column)
  125.            (error "fill-prefix too long for specified width"))
  126.            (goto-char (point-min))
  127.            (forward-line 1)
  128.            (while (not (eobp))
  129.          (if (looking-at fpre)
  130.              (delete-region (point) (match-end 0)))
  131.          (forward-line 1))
  132.            (goto-char (point-min))
  133.            (and (looking-at fpre) (forward-char (length fill-prefix)))
  134.            (setq from (point)))))
  135.       ;; from is now before the text to fill,
  136.       ;; but after any fill prefix on the first line.
  137.  
  138.       ;; Make sure sentences ending at end of line get an extra space.
  139.       ;; loses on split abbrevs ("Mr.\nSmith")
  140.       (goto-char from)
  141.       (while (re-search-forward "[.?!][])}\"']*$" nil t)
  142.     (insert ? ))
  143.  
  144.       ;; Then change all newlines to spaces.
  145.       (subst-char-in-region from (point-max) ?\n ?\ )
  146.  
  147.       ;; Flush excess spaces, except in the paragraph indentation.
  148.       (goto-char from)
  149.       (skip-chars-forward " \t")
  150.       ;; XEmacs - (ENE/stig from fa-extras.el): Skip the start of a comment.
  151.       (and comment-start-skip
  152.        (looking-at comment-start-skip)
  153.        (goto-char (match-end 0)))
  154.       ;; Nuke tabs while we're at it; they get screwed up in a fill.
  155.       ;; This is quick, but loses when a tab follows the end of a sentence.
  156.       ;; Actually, it is difficult to tell that from "Mr.\tSmith".
  157.       ;; Blame the typist.
  158.       (subst-char-in-region (point) (point-max) ?\t ?\ )
  159.       (while (re-search-forward "   *" nil t)
  160.     (delete-region
  161.      (+ (match-beginning 0)
  162.         (if (and sentence-end-double-space
  163.              (save-excursion
  164.                (skip-chars-backward " ]})\"'")
  165.                (memq (preceding-char) '(?. ?? ?!))))
  166.         2 1))
  167.      (match-end 0)))
  168.       (goto-char (point-max))
  169.       (delete-horizontal-space)
  170.       (insert "  ")
  171.       (goto-char (point-min))
  172.  
  173.       ;; This is the actual filling loop.
  174.       (let ((prefixcol 0) linebeg)
  175.     (while (not (eobp))
  176.       (setq linebeg (point))
  177.           (move-to-column (1+ fill-column))
  178.       (if (eobp)
  179.           nil
  180.         ;; Move back to start of word.
  181.         (skip-chars-backward "^ \n" linebeg)
  182.         ;; Don't break after a period followed by just one space.
  183.         ;; Move back to the previous place to break.
  184.         ;; The reason is that if a period ends up at the end of a line,
  185.         ;; further fills will assume it ends a sentence.
  186.         ;; If we now know it does not end a sentence,
  187.         ;; avoid putting it at the end of the line.
  188.         (if sentence-end-double-space
  189.                 (while (and (> (point) (+ linebeg 2))
  190.                             (eq (preceding-char) ?\ )
  191.                             (not (eq (following-char) ?\ ))
  192.                             (eq (char-after (- (point) 2)) ?\.))
  193.                   (forward-char -2)
  194.                   (skip-chars-backward "^ \n" linebeg)))
  195.  
  196.         (if (if (zerop prefixcol)
  197.             (save-excursion
  198.               (skip-chars-backward " " linebeg)
  199.               (bolp))
  200.           (>= prefixcol (current-column)))
  201.         ;; Keep at least one word even if fill prefix exceeds margin.
  202.         ;; This handles all but the first line of the paragraph.
  203.         ;; Meanwhile, don't stop at a period followed by one space.
  204.         (let ((first t))
  205.           (move-to-column prefixcol)
  206.           (while (and (not (eobp))
  207.                   (or first
  208.                   (and (not (bobp))
  209.                                        sentence-end-double-space
  210.                        (save-excursion (forward-char -1)
  211.                                (and (looking-at "\\. ")
  212.                                 (not (looking-at "\\.  ")))))))
  213.                     (skip-chars-forward " ")
  214.                     (skip-chars-forward "^ \n")
  215.             (setq first nil)))
  216.               ;; Normally, move back over the single space between the words.
  217.               (forward-char -1))
  218.       (if (and fill-prefix (zerop prefixcol)
  219.                    (< (- (point) (point-min)) (length fill-prefix))
  220.                    (string= (buffer-substring (point-min) (point))
  221.                             (substring fill-prefix 0 (- (point) (point-min)))))
  222.               ;; Keep at least one word even if fill prefix exceeds margin.
  223.               ;; This handles the first line of the paragraph.
  224.         ;; Don't stop at a period followed by just one space.
  225.         (let ((first t))
  226.           (while (and (not (eobp))
  227.                   (or first
  228.                   (and (not (bobp))
  229.                        sentence-end-double-space
  230.                                        (save-excursion (forward-char -1)
  231.                                (and (looking-at "\\. ")
  232.                                 (not (looking-at "\\.  ")))))))
  233.             (skip-chars-forward " ")
  234.             (skip-chars-forward "^ \n")
  235.             (setq first nil)))))
  236.       ;; Replace all whitespace here with one newline.
  237.       ;; Insert before deleting, so we don't forget which side of
  238.       ;; the whitespace point or markers used to be on.
  239.           (skip-chars-backward " ")
  240.           (insert ?\n)
  241.           (delete-horizontal-space)
  242.       ;; Insert the fill prefix at start of each line.
  243.       ;; Set prefixcol so whitespace in the prefix won't get lost.
  244.           (and (not (eobp)) fill-prefix (not (equal fill-prefix ""))
  245.            (progn
  246.          (insert fill-prefix)
  247.          (setq prefixcol (current-column))))
  248.       ;; Justify the line just ended, if desired.
  249.           (and justify-flag (not (eobp))
  250.            (progn
  251.          (forward-line -1)
  252.          (justify-current-line)
  253.          (forward-line 1))))))))
  254.  
  255. (defun fill-paragraph (arg)
  256.   "Fill paragraph at or after point.  Prefix arg means justify as well.
  257. If `sentence-end-double-space' is non-nil, then period followed by one
  258. space does not end a sentence, so don't break a line there."
  259.   (interactive "*P")
  260.   (let ((before (point)))
  261.     (save-excursion
  262.       (forward-paragraph)
  263.       (or (bolp) (newline 1))
  264.       (let ((end (point))
  265.             (beg (progn (backward-paragraph) (point))))
  266.         (goto-char before)
  267.     (fill-region-as-paragraph beg end arg)))))
  268.  
  269. (defun fill-region (from to &optional justify-flag)
  270.   "Fill each of the paragraphs in the region.
  271. Prefix arg (non-nil third arg, if called from program) means justify as well.
  272. If `sentence-end-double-space' is non-nil, then period followed by one
  273. space does not end a sentence, so don't break a line there."
  274.   (interactive "*r\nP")
  275.   (save-restriction
  276.    (narrow-to-region from to)
  277.    (goto-char (point-min))
  278.    (while (not (eobp))
  279.      (let ((initial (point))
  280.        (end (progn
  281.          (forward-paragraph 1) (point))))
  282.        (forward-paragraph -1)
  283.        (if (>= (point) initial)
  284.        (fill-region-as-paragraph (point) end justify-flag)
  285.      (goto-char end))))))
  286.  
  287. ;; XEmacs addition: from Tim Bradshaw <tfb@edinburgh.ac.uk>
  288. (defun fill-paragraph-or-region (arg)
  289.   "Fill the current region, if it's active; otherwise, fill the paragraph.
  290. See `fill-paragraph' and `fill-region' for more information."
  291.   (interactive "*P")
  292.   (if (and zmacs-regions (mark))
  293.       (fill-region (point) (mark) arg)
  294.     (fill-paragraph arg)))
  295.   
  296. (defun justify-current-line ()
  297.   "Add spaces to line point is in, so it ends at `fill-column'."
  298.   (interactive)
  299.   (save-excursion
  300.    (save-restriction
  301.     (let (ncols beg indent end)
  302.       (beginning-of-line)
  303.       (forward-char (length fill-prefix))
  304.       (skip-chars-forward " \t")
  305.       (setq indent (current-column))
  306.       (setq beg (point))
  307.       (end-of-line)
  308.       (narrow-to-region beg (point))
  309.       (setq end (point))
  310.       (skip-chars-backward " \t")
  311.       (delete-char (- end (point)))
  312.       (goto-char beg)
  313.       (while (re-search-forward "   *" nil t)
  314.     (delete-region
  315.      (+ (match-beginning 0)
  316.         (if (save-excursion
  317.          (skip-chars-backward " ])\"'")
  318.          (memq (preceding-char) '(?. ?? ?!)))
  319.         2 1))
  320.      (match-end 0)))
  321.       (goto-char beg)
  322.       (while (re-search-forward "[.?!][])\"']*\n" nil t)
  323.     (forward-char -1)
  324.     (insert ? ))
  325.       (goto-char (point-max))
  326.       ;; Note that the buffer bounds start after the indentation,
  327.       ;; so the columns counted by INDENT don't appear in (current-column).
  328.       (setq ncols (- fill-column (current-column) indent))
  329.       (if (search-backward " " nil t)
  330.       (while (> ncols 0)
  331.         (let ((nmove (+ 3 (random 3))))
  332.           (while (> nmove 0)
  333.         (or (search-backward " " nil t)
  334.             (progn
  335.              (goto-char (point-max))
  336.              (search-backward " ")))
  337.         (skip-chars-backward " ")
  338.         (setq nmove (1- nmove))))
  339.         (insert " ")
  340.         (skip-chars-backward " ")
  341.         (setq ncols (1- ncols)))))))
  342.   nil)
  343.  
  344. (defun fill-nonuniform-paragraphs (min max &optional justifyp mailp)
  345.   "Fill paragraphs within the region, allowing varying indentation within each.
  346. This command divides the region into \"paragraphs\",
  347. only at paragraph-separator lines, then fills each paragraph
  348. using as the fill prefix the smallest indentation of any line
  349. in the paragraph.
  350.  
  351. When calling from a program, pass range to fill as first two arguments.
  352.  
  353. Optional third and fourth arguments JUSTIFY-FLAG and MAIL-FLAG:
  354. JUSTIFY-FLAG to justify paragraphs (prefix arg),
  355. MAIL-FLAG for a mail message, i. e. don't fill header lines."
  356.   (interactive "r\nP")
  357.   (let ((fill-individual-varying-indent t))
  358.     (fill-individual-paragraphs min max justifyp mailp)))
  359.  
  360. (defun fill-individual-paragraphs (min max &optional justifyp mailp)
  361.   "Fill paragraphs of uniform indentation within the region.
  362. This command divides the region into \"paragraphs\",
  363. treating every change in indentation level as a paragraph boundary,
  364. then fills each paragraph using its indentation level as the fill prefix.
  365.  
  366. When calling from a program, pass range to fill as first two arguments.
  367.  
  368. Optional third and fourth arguments JUSTIFY-FLAG and MAIL-FLAG:
  369. JUSTIFY-FLAG to justify paragraphs (prefix arg),
  370. MAIL-FLAG for a mail message, i. e. don't fill header lines."
  371.   (interactive "r\nP")
  372.   (save-restriction
  373.     (save-excursion
  374.       (goto-char min)
  375.       (beginning-of-line)
  376.       (if mailp 
  377.       (while (or (looking-at "[ \t]*[^ \t\n]*:") (looking-at "[ \t]*$"))
  378.         (if (looking-at "[ \t]*[^ \t\n]*:")
  379.         (search-forward "\n\n" nil 'move)
  380.                 (forward-line 1))))
  381.       (narrow-to-region (point) max)
  382.       ;; Loop over paragraphs.
  383.       (while (progn (skip-chars-forward " \t\n") (not (eobp)))
  384.     (beginning-of-line)
  385.     (let ((start (point))
  386.           fill-prefix fill-prefix-regexp)
  387.       ;; Find end of paragraph, and compute the smallest fill-prefix
  388.       ;; that fits all the lines in this paragraph.
  389.       (while (progn
  390.            ;; Update the fill-prefix on the first line
  391.            ;; and whenever the prefix good so far is too long.
  392.            (if (not (and fill-prefix
  393.                  (looking-at fill-prefix-regexp)))
  394.                (setq fill-prefix
  395.                  (buffer-substring (point)
  396.                            (save-excursion
  397.                          (skip-chars-forward " \t")
  398.                          (point)))
  399.                  fill-prefix-regexp
  400.                  (regexp-quote fill-prefix)))
  401.            (forward-line 1)
  402.            ;; Now stop the loop if end of paragraph.
  403.            (and (not (eobp))
  404.             (if fill-individual-varying-indent
  405.                 ;; If this line is a separator line, with or
  406.                 ;; without prefix, end the paragraph.
  407.                 (and 
  408.                              (not (looking-at paragraph-separate))
  409.                              (save-excursion
  410.                                (not (and (looking-at fill-prefix-regexp)
  411.                                          (progn
  412.                        (forward-char (length fill-prefix))
  413.                        (looking-at paragraph-separate))))))
  414.                             ;; If this line has more or less indent
  415.                             ;; than the fill prefix wants, end the paragraph.
  416.                             (and (looking-at fill-prefix-regexp)
  417.                                  (save-excursion
  418.                                    (not
  419.                     (progn
  420.                       (forward-char (length fill-prefix))
  421.                       (or (looking-at paragraph-separate)
  422.                       (looking-at paragraph-start))))))))))
  423.       ;; Fill this paragraph, but don't add a newline at the end.
  424.       (let ((had-newline (bolp)))
  425.         (fill-region-as-paragraph start (point) justifyp)
  426.         (or had-newline (delete-char -1))))))))
  427.  
  428. ;;; fill.el ends here
  429.